package com.gamecauca.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.gamecauca.Assets;
import com.gamecauca.Input;
import com.gamecauca.common.Constant;
import com.gamecauca.common.GameConfig;
import com.gamecauca.common.OverlapTester;


public class CatalogScreen extends Screen {

	OrthographicCamera camera;
	Vector3 touchPoint;
	SpriteBatch batch;
	
	boolean upHighlight = false;
	boolean downHighlight = false;
	boolean backHighlight = false;
	
	Rectangle upTab;
	Rectangle downTab;
	Rectangle backTab;
	
	Rectangle fishTab;
	Rectangle itemTab;
	Rectangle tabBound;
	
	public String strTab = "fish";
	
	public int fishSelector = 0;
	public int itemSelector = 0;
	
	public int fishIdTab = -1;
	public int itemIdTab = -1;
	
//	public FishMoneys fishMoney = new FishMoneys();
	
	public CatalogScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
		camera = new OrthographicCamera(Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
		camera.position.set(Constant.SCREEN_WIDTH/2, Constant.SCREEN_HEIGHT/2, 0);
		touchPoint = new Vector3();
		batch = new SpriteBatch();
		
		upTab = new Rectangle(Constant.SCREEN_WIDTH/2 - 64, Constant.SCREEN_HEIGHT - 64, 128, 64);
		downTab = new Rectangle(Constant.SCREEN_WIDTH/2 - 64, 128, 128, 64);
		backTab = new Rectangle(40, 10, 128, 64);
		
		fishTab = new Rectangle((Constant.SCREEN_WIDTH/2 - 85), (Constant.SCREEN_HEIGHT - 128 - 32), 128, 64);
		itemTab = new Rectangle((Constant.SCREEN_WIDTH/2 - 85), (Constant.SCREEN_HEIGHT - 256), 128, 64);
	
		tabBound = new Rectangle();
	}

	public void tick(Input input) {
		
//		input.buttons[Input.CREDITS] = false;
		input.releaseAllKeys();
	}
	
	@Override
	public void present() {
		// TODO Auto-generated method stub
		
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.enableBlending();
		batch.begin();
		
		batch.draw(Assets.catalogBackgroundRegion, 0, 0);
		
		batch.draw(upHighlight ? Assets.ITEM_TYPE_1[1][0] : Assets.ITEM_TYPE_1[0][0], Constant.SCREEN_WIDTH/2 - 64, Constant.SCREEN_HEIGHT - 64);
		batch.draw(downHighlight ? Assets.ITEM_TYPE_2[1][0] : Assets.ITEM_TYPE_2[0][0], Constant.SCREEN_WIDTH/2 - 64, 128);
		batch.draw(backHighlight ? Assets.backButtonRegion[1] : Assets.backButtonRegion[0], 40, 10);
		
		batch.draw(Assets.fishTabRegion, (Constant.SCREEN_WIDTH/2 - 85), (Constant.SCREEN_HEIGHT - 128 - 32));
		batch.draw(Assets.itemTabRegion, (Constant.SCREEN_WIDTH/2 - 85), (Constant.SCREEN_HEIGHT - 256));
		
		batch.draw(Assets.catalogContainerRegion, Constant.SCREEN_WIDTH/2 + 32, Constant.SCREEN_HEIGHT - 96);
		batch.draw(Assets.catalogContainerRegion, Constant.SCREEN_WIDTH/2 + 32, Constant.SCREEN_HEIGHT - 96 - 1*(5 + 85));
		batch.draw(Assets.catalogContainerRegion, Constant.SCREEN_WIDTH/2 + 32, Constant.SCREEN_HEIGHT - 96 - 2*(5 + 85));
		batch.draw(Assets.catalogContainerRegion, Constant.SCREEN_WIDTH/2 + 32, Constant.SCREEN_HEIGHT - 96 - 3*(5 + 85));
		
		drawTabInfo(strTab);			
		drawTabDetails(strTab);		
		
		batch.end();
		
		renderCatalogTab();
	}
	
	public void renderCatalogTab(){
		
		if (Gdx.input.justTouched()) {
			
			camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			
			if(OverlapTester.pointInRectangle(backTab, touchPoint.x, touchPoint.y)){
				
				backHighlight = true;
				this.game.setScreen(new MainMenuScreen(game));
			}
			
			if(OverlapTester.pointInRectangle(upTab, touchPoint.x, touchPoint.y))
			{
				upHighlight = true;
				downHighlight = false;
				
				if(strTab.equals("fish")){
					
					if(fishSelector > 0)
						fishSelector--;
				}
				else if(strTab.equals("item")){
					
					if(itemSelector > 0)
						itemSelector--;
				}
			}
			
			if(OverlapTester.pointInRectangle(downTab, touchPoint.x, touchPoint.y))
			{
				upHighlight = false;
				downHighlight = true;
				
				if(strTab.equals("fish")){
					
					if(fishSelector*4 + 4 < Assets.fish.length)
						fishSelector++;	
				}
				else if(strTab.equals("item")){
					
					if(itemSelector*4 + 4 < Assets.itemList.size())
						itemSelector++;
					
				}
			}
		
			if(OverlapTester.pointInRectangle(fishTab, touchPoint.x, touchPoint.y))
			{
				strTab = "fish";
			}
			
			if(OverlapTester.pointInRectangle(itemTab, touchPoint.x, touchPoint.y))
			{
				strTab = "item";
			}
			
			if(strTab.equals("fish")){
				
				int end = fishSelector * 4 + 4;
				int yCount = 0;
				
				if(end > Assets.fish.length)
					end = Assets.fish.length;
				
				for(int i = fishSelector * 4; i < end; i++){
					
					tabBound.set(Constant.SCREEN_WIDTH/2 + 32, Constant.SCREEN_HEIGHT - 96 - yCount*(5 + 85), 355, 85);
					yCount++;
					
					if(OverlapTester.pointInRectangle(tabBound, touchPoint.x, touchPoint.y))
					{
//						System.out.println(Assets.fishInfoList.get(i).getName());
						fishIdTab = i;
					}
				}
			}
			else if(strTab.equals("item")){
				
				int end = itemSelector * 4 + 4;
				int yCount = 0;
				
				if(end > Assets.itemList.size())
					end = Assets.itemList.size();
				
				for(int i = itemSelector * 4; i < end; i++){
					
					tabBound.set(Constant.SCREEN_WIDTH/2 + 32, Constant.SCREEN_HEIGHT - 96 - yCount*(5 + 85), 355, 85);
					yCount++;
					
					if(OverlapTester.pointInRectangle(tabBound, touchPoint.x, touchPoint.y))
					{
//						System.out.println(Assets.itemList.get(i).getName());
						itemIdTab = i;
					}
				}
			}
			
		}
	}
	
	public void drawTabInfo(String strTab){
		
		int x = Constant.SCREEN_WIDTH/2 + 32;
		int yCount = 0;
		int start = 0;
		int end = 0;
		
		if(strTab.equals("fish")){
			
			start = fishSelector * 4;
			end = start + 4;
			
			if(end > Assets.fish.length)
				end = Assets.fish.length;
			
			for(int i = start; i < end; i++){
				
				batch.draw(Assets.fishRegionTabInfo[i][0], x + 275, Constant.SCREEN_HEIGHT - 96 - yCount*(85));  
				float width = Assets.font.getBounds(String.valueOf(GameConfig.getGiaTri(i))).width;
				
				Assets.font.draw(batch,String.valueOf(GameConfig.getGiaTri(i)), x + 55, Constant.SCREEN_HEIGHT - 45 - yCount*(5 + 85));
				Assets.font.draw(batch, Assets.fishInfoList.get(i).getName(), x + 140, Constant.SCREEN_HEIGHT - 45 - yCount*(5 + 85));
				
				yCount++;
			}
		}
		else if(strTab.equals("item")){
			
			start = itemSelector * 4;
			end = start + 4;
			
			if(end > Assets.itemList.size())
				end = Assets.itemList.size();
			
			for(int i = start; i < end; i++){
				
				batch.draw(Assets.itemImgs[Assets.itemList.get(i).getId()], x + 275, Constant.SCREEN_HEIGHT - 96 - yCount*(85));
				float width = Assets.font.getBounds(String.valueOf(Assets.itemList.get(i).getPrice())).width;
				
				Assets.font.draw(batch,String.valueOf(Assets.itemList.get(i).getPrice()), x + 55, Constant.SCREEN_HEIGHT - 45 - yCount*(5 + 85));
				Assets.font.draw(batch,String.valueOf(Assets.itemList.get(i).getName()), x + 140, Constant.SCREEN_HEIGHT - 45 - yCount*(5 + 85));
				
				yCount++;
			}
		}
		
	}
	
	public void drawTabDetails(String strTab){
		
		if(strTab.equals("fish") && fishIdTab >= 0){
			
			Assets.font.draw(batch, Assets.fishInfoList.get(fishIdTab).getName(), 40, Constant.SCREEN_HEIGHT - 110);
			batch.draw(Assets.gold, 35, Constant.SCREEN_HEIGHT - 110 - 60);
			Assets.font.draw(batch,String.valueOf(GameConfig.getGiaTri(fishIdTab)), 80, Constant.SCREEN_HEIGHT - 110 - 35);
			
			batch.draw(Assets.fishRegionTabInfo[fishIdTab][0], 40 + 180, Constant.SCREEN_HEIGHT - 110 - 60);
			
			String[] details = Assets.fishInfoList.get(fishIdTab).getDetails().split("\n");
			
			for(int i = 0 ; i < details.length; i++){
				
				Assets.font.draw(batch,details[i], 40, Constant.SCREEN_HEIGHT - 110 - 100 - (i*40));
				
			}
			
			
		}
		else if(strTab.equals("item") && itemIdTab >= 0){
			
			Assets.font.draw(batch,String.valueOf(Assets.itemList.get(itemIdTab).getName()), 40, Constant.SCREEN_HEIGHT - 110);
			batch.draw(Assets.gold, 35, Constant.SCREEN_HEIGHT - 110 - 60);
			Assets.font.draw(batch,String.valueOf(Assets.itemList.get(itemIdTab).getPrice()), 80, Constant.SCREEN_HEIGHT - 110 - 35);
			
			batch.draw(Assets.itemImgs[Assets.itemList.get(itemIdTab).getId()], 40 + 180, Constant.SCREEN_HEIGHT - 110 - 60);
			
			String[] details = Assets.itemList.get(itemIdTab).getInfo().split("\n");
			
			for(int i = 0 ; i < details.length; i++){
				
				Assets.font.draw(batch,details[i], 40, Constant.SCREEN_HEIGHT - 110 - 100 - (i*40));
			}
			
		}
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
